package com.situ.springbootscientific.config;

import com.situ.springbootscientific.converter.String2DateConverter;
import com.situ.springbootscientific.interceptor.LoginInterceptor;
import com.situ.springbootscientific.pojo.Researchers;
import com.situ.springbootscientific.service.IAuthService;
import com.situ.springbootscientific.service.IResearchersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    IAuthService authService;
    @Autowired
    IResearchersService researchersService;
    @Bean
    public Researchers createResearchers() {
        return new Researchers();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**")
                .addResourceLocations("file:/E:/IdeaProjects/mypic/blog/blog/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new String2DateConverter());
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor(researchersService, authService)).
                addPathPatterns("/**")
                .excludePathPatterns("/auth/code","/researchers/login","/page/login","researchers/logout","/static/**");
    }
}
